package com.xlh.controller.user;

import com.google.common.collect.Lists;
import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.system.OperationUserDTO;
import com.xlh.enums.user.EventCodeEnum;
import com.xlh.param.user.OperationParam;
import com.xlh.security.SpringSecurityUserInfoHolder;
import com.xlh.service.system.OperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 流水
 * <p>
 * Created by lx on 2019/1/25.
 */
@Api(tags = "OperationController", description = "流水")
@RestController
@RequestMapping("/operation")
@PreAuthorize("@permissionValidator.isAdmin()")
public class OperationController {

    @Autowired
    private OperationService operationService;

    @ApiOperation("获取流水列表分页")
    @GetMapping("/page")
    public Result<PageDTO<OperationUserDTO>> listOperationByPage(
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "页数") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页显示条数") Integer pageSize,
            @RequestParam(required = false) @ApiParam(value = "用户信息") String userInfo,
            @RequestParam(required = false) @ApiParam(value = "角色名称") String roleName,
            @RequestParam(required = false) @ApiParam(value = "学校名称") String schoolName,
            @RequestParam(required = false) @ApiParam(value = "开始时段") String startTime,
            @RequestParam(required = false) @ApiParam(value = "结束时段") String endTime) {
        return Result.success(
                operationService.listOperationByPage(
                        pageNum, pageSize, userInfo, roleName, schoolName, startTime, endTime, Lists.newArrayList(
                                EventCodeEnum.LOGIN, EventCodeEnum.LOGOUT, EventCodeEnum.CHAPTER_RECORDING))
        );
    }

    @ApiOperation("流水批量删除")
    @PostMapping("/batch-delete")
    public Result batchDeleteOperation(@RequestBody @ApiParam(value = "流水id集合", required = true) List<Long> ids) {
        operationService.batchDeleteOperation(ids);
        return Result.success();
    }

    @ApiOperation("下载流水")
    @PostMapping("/export")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result exportExcel(@RequestBody @Valid OperationParam param) {
        operationService.exportExcel(param.getUserInfo(), param.getRoleName(), param.getSchoolName(),
                param.getStartTime(), param.getEndTime(),
                Lists.newArrayList(EventCodeEnum.LOGIN, EventCodeEnum.LOGOUT, EventCodeEnum.CHAPTER_RECORDING));
        return Result.success();
    }

    @ApiOperation("查看已完成的学习记录")
    @GetMapping("/chapter/{chapterId}/studied")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result<Boolean> queryChapterStudied(
            @PathVariable("chapterId") @ApiParam(value = "章节id") Long chapterId,
            @Autowired SpringSecurityUserInfoHolder userInfoHolder) {
        List<Long> chapterIds = operationService.queryOperationIds(
                userInfoHolder.getUserId(), Lists.newArrayList(chapterId), EventCodeEnum.CHAPTER_STUDIED);
        if (CollectionUtils.isEmpty(chapterIds)) {
            return Result.success(false);
        }
        return Result.success(true);
    }

}
